#!/usr/bin/env php
<?php
//declare(ticks = 1);
require __DIR__.'/../vendor/autoload.php';

//定义根目录
define("HOME", dirname(__DIR__));
//时间区设定
if (!date_default_timezone_get() || !ini_get("date.timezone")) {
	date_default_timezone_set("PRC");
}

//启动命令所需参数
$str_argv = '';
for ($i = 1; $i < $argc; $i++) {
    $str_argv .= ' ' . $argv[$i];
}

//启动命令所需参数
$file_name = str_replace(__DIR__, "", __FILE__);
$file_name = trim($file_name, "/");
$file_name = trim($file_name, "\\");

//启动命令
$command_line = 'php ' . $file_name . ' ' . $str_argv;
define("WING_COMMAND_LINE", $command_line);

//debug模式解析，来源于--debug参数
$debug = false;
foreach ($argv as $item) {
	if (strpos($item, "debug") !== false) {
		$debug = true;
	}
}
define("WING_DEBUG", $debug);

//命令行参数解析
$start_pos   = 0;
$end_pos     = 0;
$event_index = 0;

foreach ($argv as $item) {
	if (strpos($item,"--start") === 0) {
	    list(,$start_pos) = explode("=",$item);
	}

	if (strpos($item,"--end") === 0) {
	    list(,$end_pos) = explode("=",$item);
	}

    if (strpos($item,"--event_index") === 0) {
	    list(,$event_index) = explode("=",$item);
	}
}

//pdo对象
$pdo      = new \Wing\Library\PDO();
//binlog对象
$bin      = new \Wing\Library\Binlog($pdo);
//获取原始数据
$raw_data = $bin->getSessions($start_pos, $end_pos);
//解析原始数据，得到事件数据（数组）
$file     = new \Wing\Library\FileFormat($raw_data, $pdo, $event_index);
$datas    = $file->parse();

//结果输出
echo json_encode($datas);